package com.cet.spring_batch_study.jobs.cooking;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.support.SimpleFlow;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import java.util.concurrent.TimeUnit;

/**
 *
 * 洗菜和烧水并行执行
 * @author NSOC
 */

@Configuration
public class CookingJob {

    @Bean
    public Job cooking(JobRepository jobRepository, Flow flow1, Flow flow2,
                       Step doCooking,
                       Step plating) {
        return new JobBuilder("cooking", jobRepository)
                .start(flow1)
                .split(new SimpleAsyncTaskExecutor())
                .add(flow2)
                .next(doCooking)
                .next(plating)
                .end()
                .build();
    }

    @Bean
    public Flow flow1(Step heatWater) {
        return new FlowBuilder<SimpleFlow>("flow1")
                .start(heatWater)
                .build();
    }

    @Bean
    public Flow flow2(Step washFoods) {
        return new FlowBuilder<SimpleFlow>("flow2")
                .start(washFoods)
                .build();
    }


    @Bean
    public Step heatWater(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("烧水", jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("正在烧水……");
                    TimeUnit.SECONDS.sleep(1);
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .build();
    }

    @Bean
    public Step washFoods(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("洗菜", jobRepository)
                .tasklet(((contribution, chunkContext) -> {
                    System.out.println("正在洗菜……");
                    TimeUnit.SECONDS.sleep(1);
                    return RepeatStatus.FINISHED;
                }), transactionManager)
                .build();
    }

    @Bean
    public Step doCooking(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("烹饪", jobRepository)
                .tasklet(((contribution, chunkContext) -> {
                    System.out.println("开始烹饪");
                    TimeUnit.SECONDS.sleep(5);
                    return RepeatStatus.FINISHED;
                }), transactionManager)
                .build();
    }

    @Bean
    public Step plating(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("装盘", jobRepository)
                .tasklet(((contribution, chunkContext) -> {
                    System.out.println("盛起装盘");
                    return RepeatStatus.FINISHED;
                }), transactionManager)
                .build();
    }

}
